package com.sgb.kjwl.viewmodel.packers;

import android.app.Application;
import android.text.TextUtils;

import androidx.lifecycle.AndroidViewModel;
import androidx.lifecycle.MutableLiveData;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.ServicesCompanyEntity;
import com.sgb.kjwl.model.entity.user.AddressEntity;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.PickSelectEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
*@author 李昭
*@date 2020/9/18
*description:
*/
public class ServicesCompanyModel extends AndroidViewModel {

    private int page = 1;
    private int limit = 10;

    private PickSelectEntity all;

    public MutableLiveData<Boolean> loading = new MutableLiveData<>();
    public MutableLiveData<List<ServicesCompanyEntity>> laborList = new MutableLiveData<>();
    public MutableLiveData<List<PickSelectEntity>> typeList = new MutableLiveData<>();
    public MutableLiveData<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> cityList = new MutableLiveData<>();

    public ServicesCompanyModel(Application application) {
        super(application);
        all = new PickSelectEntity();
        all.setNo("");
        all.setProjectscale("全部");
    }

    /**
     * 列表
     * @param companyType
     * @param provinceNo
     * @param cityNo
     * @param areaNo
     * @param b
     */
    public void getNewList(String companyType, String provinceNo, String cityNo, String areaNo, boolean b) {
        Map<String,Object> map = new HashMap<>();
        page = b ? page + 1 : 1;
        map.put("pageSize", limit);
        map.put("pageNum", page);

        if (!TextUtils.isEmpty(companyType) && !TextUtils.equals("全部", companyType)) {
            map.put("companyType", companyType);
        }
        if (!TextUtils.isEmpty(provinceNo)) {
            map.put("provinceNo", provinceNo);
        }
        if (!TextUtils.isEmpty(cityNo)) {
            map.put("cityNo", cityNo);
        }
        if (!TextUtils.isEmpty(areaNo)) {
            List<String> stringList = new ArrayList<>();
            stringList.add(areaNo);
            map.put("areaNoList", stringList);
        }
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getServicesCompany(map).enqueue(new ICallBack<BaseEntity<List<ServicesCompanyEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<ServicesCompanyEntity>> data) {
                DialogHelper.dismissProgressDialog();
                laborList.postValue(data.getData());
            }
        });
    }

    // ------------------------------------------企业类型 -----------------------------------------------


    public void getCompanyTypeModel() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCompanyType().enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data1) {
                DialogHelper.dismissProgressDialog();
                List<PickSelectEntity> data = data1.getData();
                if (data == null || data.size() == 0) {
                    data = new ArrayList<>();
                }
                data.add(0, all);
                typeList.postValue(data);
            }
        });
    }

    public void getCityList() {
        loading.postValue(true);
        APIManager.getInstance().getPackerAPI().getCityList2().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                DialogHelper.dismissProgressDialog();
                cityList.postValue(data.getData());
            }
        });
    }
}
